package com.yndsoft.jc.supplier.join;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yndsoft.jc.R;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.base.SupplierBaseActivity;
import com.yndsoft.jc.supplier.net.util.ProgressSubscriber;
import com.yndsoft.jc.supplier.utils.IsInputWatcher;
import com.yndsoft.jc.supplier.utils.CountDownTimerUtil;
import com.yndsoft.jc.supplier.utils.ToastUtil;
import com.yndsoft.jc.supplier.widget.dialog.SupplierDialog;

public class JoinActivity extends SupplierBaseActivity implements View.OnClickListener {

    private RelativeLayout joinDel;
    private EditText joinPhone;
    private EditText joinCode;
    private Button joinSend;
    private EditText joinPwd;
    private CountDownTimerUtil countDownTimer;
    private String verifyCode;

    @Override
    public int getContentResId() {
        return R.layout.activity_join;
    }

    public static void start(Context context) {
        context.startActivity(new Intent(context, JoinActivity.class));
    }

    @Override
    public void initView() {
        ImageButton barBack = findViewById(R.id.bar_back);
        TextView barTitle = findViewById(R.id.bar_title);
        joinPhone = findViewById(R.id.join_phone);
        joinDel = findViewById(R.id.join_del);
        joinCode = findViewById(R.id.join_code);
        joinSend = findViewById(R.id.join_send);
        joinPwd = findViewById(R.id.join_pwd);
        Button regNext = findViewById(R.id.join_next);

        barTitle.setText("机构入驻");
        barBack.setOnClickListener(this);
        joinDel.setOnClickListener(this);
        joinSend.setOnClickListener(this);
        regNext.setOnClickListener(this);
        joinPhone.addTextChangedListener(isInput);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bar_back:
                finish();
                break;
            case R.id.join_del:
                joinPhone.setText("");
                break;
            case R.id.join_send:
                String phoneSend = joinPhone.getText().toString().trim();
                if (!TextUtils.isEmpty(phoneSend) && phoneSend.length() == 11) {
                    getVerifyCode(phoneSend);
                    countDownTimer = new CountDownTimerUtil(joinSend);
                    countDownTimer.start();
                } else {
                    ToastUtil.show(mContext, "请输入正确的手机号！");
                }
                break;
            case R.id.join_next:
                String phoneValue = joinPhone.getText().toString().trim();
                String codeValue = joinCode.getText().toString().trim();
                String pwdValue = joinPwd.getText().toString();
                if (TextUtils.isEmpty(phoneValue)) {
                    showHint("手机号不能为空！");
                } else {
                    if (phoneValue.length() != 11) {
                        showHint("请输入正确的手机号！");
                    } else {
                        if (TextUtils.isEmpty(codeValue)) {
                            showHint("验证码不能为空！");
                        } else {
                            if (codeValue.length() != 6) {
                                showHint("请输入正确的验证码！");
                            } else {
                                if (!codeValue.equals(verifyCode)) {
                                    showHint("验证码错误！");
                                } else {
                                    if (TextUtils.isEmpty(pwdValue)) {
                                        showHint("密码不能为空！");
                                    } else {
                                        if (pwdValue.length() < 6 || pwdValue.length() > 20) {
                                            showHint("请输入6-20位密码！");
                                        } else {
                                            JoinInfoActivity.start(mContext, phoneValue, pwdValue);
                                            finish();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                break;
        }
    }

    private void getVerifyCode(String phoneSend) {
        mHttpUtil.verifyCode(new ProgressSubscriber(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:
                    case 2:
                        showHint(returnMsg.getInfo() != null ? returnMsg.getInfo() : "该手机号已存在！");
                        break;
                    case 1:
                        verifyCode = returnMsg.getInfo();
                        break;
                }
            }
        }, "shortmsg", phoneSend, 3);
    }

    private IsInputWatcher isInput = new IsInputWatcher() {
        @Override
        public void noInput() {
            joinDel.setVisibility(View.INVISIBLE);
        }

        @Override
        public void inputting() {
            joinDel.setVisibility(View.VISIBLE);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }
}